package com.jichaoyun.sys.service.impl;//package com.jichaoyun.sys.service.impl;
//
//import com.alibaba.druid.support.json.JSONUtils;
//import com.alibaba.excel.EasyExcel;
//import com.alibaba.excel.util.MapUtils;
//import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.jichaoyun.model.entity.StationInfo;

//import com.jichaoyun.model.steam.StationInspection;
//import com.jichaoyun.common.enums.MyExceptiontType;
//import com.jichaoyun.common.ex.MyException;
//import com.mapper.sys.com.jichaoyun.StationInspectionMapper;
//import com.service.sys.com.jichaoyun.IStationInfoService;
//import com.jichaoyun.sys.service.StationInspectionService;
//import com.jichaoyun.common.utils.ExcelStyleUtils;
//import com.jichaoyun.common.model.PagedResult;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.net.URLEncoder;
//import java.time.LocalDate;
//import java.time.LocalDateTime;
//import java.time.LocalTime;
//import java.time.format.DateTimeFormatter;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
//* @author ljc
//* @description 针对表【station_inspection】的数据库操作Service实现
//* @createDate 2023-12-09 13:42:05
//*/
//@Service
//public class StationInspectionServiceImpl extends ServiceImpl<StationInspectionMapper, StationInspection>
//    implements StationInspectionService {
//
//    @Autowired
//    private StationInspectionMapper stationInspectionMapper;
//
//    @Autowired
//    private IStationInfoService stationInfoService;
//
//    /**
//     * 根据日期返回巡检记录
//     * @param date
//     * @return
//     */
//    @Override
//    public PagedResult getStationInspectionInfo(LocalDate date,Integer pageNo,Integer pageSize) {
//
//        List<Object> list = new ArrayList<>();
//        LocalDateTime day_start = LocalDateTime.of(date, LocalTime.MIN);
//
//        IPage<StationInfo> infoIPage = stationInfoService.getAllStationInfo(pageNo,pageSize);
//        List<StationInfo> stationInfos = infoIPage.getRecords();
//        PagedResult pagedResult = new PagedResult();
//        pagedResult.setTotal((int) infoIPage.getTotal());
//        pagedResult.setPages((int) infoIPage.getPages());
//
//        for(StationInfo item: stationInfos){
//            LambdaQueryWrapper<StationInspection> wrapper = new LambdaQueryWrapper<>();
//            wrapper.gt(StationInspection::getInspectTime,day_start)
//                    .le(StationInspection::getInspectTime,day_start.plusDays(1))
//                    .eq(StationInspection::getStationNo,item.getStationNo());
//            List<StationInspection> resList = this.stationInspectionMapper.selectList(wrapper);
//
//            Map<String,List<Object>> resMap = new HashMap<>();
//            List<Object> res = new ArrayList<>();
//
//            for(StationInspection item1 : resList){
//               res.add(item1.getInspectTime().toString());
//            }
//            resMap.put(item.getStationName(),res);
//            list.add(resMap);
//        }
//
//        pagedResult.setRows(list);
//
//        return pagedResult;
//    }
//
//    @Override
//    public List<StationInspection> getAllStationInspection() {
//        LambdaQueryWrapper<StationInspection> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.orderByAsc(StationInspection::getStationNo);
//        List<StationInspection> resList = this.stationInspectionMapper.selectList(lambdaQueryWrapper);
//
//        return resList;
//    }
//
//    @Override
//    public List<StationInspection> getAllStationInspection(LocalDate dateTime) {
//        LambdaQueryWrapper<StationInspection> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.between(StationInspection::getInspectTime,dateTime,dateTime.plusDays(1))
//                .orderByAsc(StationInspection::getStationNo);
//        List<StationInspection> resList = this.stationInspectionMapper.selectList(lambdaQueryWrapper);
//
//        return resList;
//    }
//
//    /**
//     * 某日巡检信息导出到excel
//     * @param date
//     * @param response
//     * @throws IOException
//     */
//    @Override
//    public void getStationInspectionExcel(String date, HttpServletResponse response) throws IOException {
//        // 定义日期格式
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        // 将字符串转换为 LocalDate
//        LocalDate theDate = LocalDate.parse(date, formatter);
//
//        List<StationInspection> resList = getAllStationInspection(theDate);
//        // 创建 DateTimeFormatter 对象，定义日期格式
//        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy年M月d日 ");
//        // 使用 DateTimeFormatter 格式化 LocalDateTime 对象
//        String formattedDate = theDate.format(formatter1);
//
//        try {
//            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//            response.setCharacterEncoding("utf-8");
//            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
//            String fileName = URLEncoder.encode(formattedDate + "巡检信息表", "UTF-8").replaceAll("\\+", "%20");
//            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
//            HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle());
//
//            // 这里需要设置不关闭流
//            EasyExcel.write(response.getOutputStream(), StationInspection.class)
//                    .registerWriteHandler(horizontalCellStyleStrategy)
//                    .autoCloseStream(Boolean.FALSE)
//                    .sheet("sheet1")
//                    .doWrite(resList);
//
//        } catch (Exception e) {
//            // 重置response
//            response.reset();
//            response.setContentType("application/json");
//            response.setCharacterEncoding("utf-8");
//            Map<String, String> map = MapUtils.newHashMap();
//            map.put("status", "failure");
//
//            map.put("message", "下载文件失败" + e.getMessage());
//            response.getWriter().println(JSONUtils.toJSONString(map));
//            throw new MyException(MyExceptiontType.FILEDOWNLOADFAIL,"下载文件失败");
//        }
//    }
//
//    /**
//     * 历史巡检信息导出到excel
//     * @param response
//     * @throws IOException
//     */
//    @Override
//    public void getStationInspectionExcel(HttpServletResponse response) throws IOException {
//
//        List<StationInspection> resList = getAllStationInspection();
//
//        try {
//            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//            response.setCharacterEncoding("utf-8");
//            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
//            String fileName = URLEncoder.encode("历史巡检信息表", "UTF-8").replaceAll("\\+", "%20");
//            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
//            HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle());
//
//            // 这里需要设置不关闭流
//            EasyExcel.write(response.getOutputStream(), StationInspection.class)
//                    .registerWriteHandler(horizontalCellStyleStrategy)
//                    .autoCloseStream(Boolean.FALSE)
//                    .sheet("sheet1")
//                    .doWrite(resList);
//
//        } catch (Exception e) {
//            // 重置response
//            response.reset();
//            response.setContentType("application/json");
//            response.setCharacterEncoding("utf-8");
//            Map<String, String> map = MapUtils.newHashMap();
//            map.put("status", "failure");
//
//            map.put("message", "下载文件失败" + e.getMessage());
//            response.getWriter().println(JSONUtils.toJSONString(map));
//            throw new MyException(MyExceptiontType.FILEDOWNLOADFAIL,"下载文件失败");
//        }
//    }
//}
//
//
//
//
